//
//  SAInformationFeedbackViewController.m
//  SunOn
//
//  Created by mc on 2017/5/3.
//  Copyright © 2017年 NeoZhang. All rights reserved.
//  信息反馈界面  分类反馈  例:同业信息

#import "SAInformationFeedbackViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

#import "SACollectionInformationFirstCell.h"
#import "SACollectionInformationSecondCell.h"
#import "SACollectionInformationThirdCell.h"

@interface SAInformationFeedbackViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource>
{
    UIImagePickerController *_imagePickerController;
}


@property (weak, nonatomic) IBOutlet UITableView *tabview;

@property (nonatomic, strong) NSMutableArray *imageArr;

@end

@implementation SAInformationFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
    _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    _imagePickerController.allowsEditing = YES;
    [self.tabview registerNib:[UINib nibWithNibName:@"SACollectionInformationFirstCell" bundle:nil] forCellReuseIdentifier:@"SACollectionInformationFirstCell"];
    [self.tabview registerNib:[UINib nibWithNibName:@"SACollectionInformationSecondCell" bundle:nil] forCellReuseIdentifier:@"SACollectionInformationSecondCell"];
    [self.tabview registerNib:[UINib nibWithNibName:@"SACollectionInformationThirdCell" bundle:nil] forCellReuseIdentifier:@"SACollectionInformationThirdCell"];
    self.tabview.delegate = self;
    self.tabview.dataSource = self;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < 3) {
        SACollectionInformationFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SACollectionInformationFirstCell" forIndexPath:indexPath];
        switch (indexPath.row) {
            case 0:
            {
                cell.titleLB.text = @"信息分类:";
                cell.contentLB.text = @"产品信息";
                cell.jiantouImage.hidden = YES;
            }
                break;
            case 1:
            {
                cell.titleLB.text = @"反馈到部门:";
                cell.contentLB.text = @"研发部";
                cell.jiantouImage.hidden = NO;
            }
                break;
            default:
            {
                cell.titleLB.text = @"人员:  ";
                cell.contentLB.text = @"任盈盈";
                cell.jiantouImage.hidden = NO;
            }
                break;
        }
        return cell;
    }else {
        if (indexPath.row == 3) {
            SACollectionInformationSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SACollectionInformationSecondCell" forIndexPath:indexPath];
            return cell;
        }
        SACollectionInformationThirdCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SACollectionInformationThirdCell" forIndexPath:indexPath];
        return cell;
    }
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 4) {
        //照相
        
    }
    if (indexPath.row == 1) {
        //更多部门
        
    }
    if (indexPath.row == 2) {
        //更多人员
        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < 3) {
        return 44;
    }else {
        if (indexPath.row == 3) {
            return SASCREEN_HEIGHT - 44 * 3 - 90 - 64;
        }else {
            return 90;
        }
    }
}

#pragma mark - view method

#pragma mark - view cycle
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UIBarButtonItem *cancle = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStyleDone) target:self action:@selector(handleCancle)];
    [cancle setTintColor:[UIColor blackColor]];
    self.navigationItem.leftBarButtonItem = cancle;
    UIBarButtonItem *send = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:(UIBarButtonItemStylePlain) target:self action:@selector(handleSend)];
    self.navigationItem.rightBarButtonItem = send;
    [send setTintColor:[UIColor colorWithHexString:@"#0d88e8"]];
}
#pragma mark - custom method
- (void)handleCancle {
    [self.navigationController popViewControllerAnimated:YES];
}
//发送反馈信息
- (void)handleSend {
    
}

#pragma mark 从摄像头获取图片或视频
- (void)selectImageFromCamera
{
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    //设置摄像头模式（拍照，录制视频）为录像模式
    _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    [self presentViewController:_imagePickerController animated:YES completion:nil];
}

#pragma mark 从相册获取图片或视频
- (void)selectImageFromAlbum
{
    //NSLog(@"相册");
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:_imagePickerController animated:YES completion:nil];
}

#pragma mark UIImagePickerControllerDelegate
//该代理方法仅适用于只选取图片时
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    NSLog(@"选择完毕----image:%@-----info:%@",image,editingInfo);
}

//适用获取所有媒体资源，只需判断资源类型
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    //判断资源类型
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
        //图片
        [self.imageArr addObject:info[UIImagePickerControllerEditedImage]];
//        self.imageView.image = info[UIImagePickerControllerEditedImage];
        //压缩图片
//        NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0);
        //保存图片至相册
//        UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }else{
        //视频
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark 图片保存完毕的回调
- (void) image: (UIImage *) image didFinishSavingWithError:(NSError *) error contextInfo: (void *)contextInf{
    
}
#pragma mark - setter&getter
- (NSMutableArray *)imageArr {
    if (!_imageArr) {
        self.imageArr = [NSMutableArray array];
    }
    return _imageArr;
}

@end
